package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sixth.bean.MarketKeyword;
import com.sixth.bean.MarketKeywordExample;
import com.sixth.bean.bo.KeywordCreateBO;
import com.sixth.bean.bo.KeywordListBO;
import com.sixth.bean.bo.KeywordBO;
import com.sixth.bean.vo.KeywordListVO;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketKeywordMapper;
import com.sixth.service.AdminKeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ljh
 * @version 1.0
 * @describition:
 * @since 2022/9/7 14:44
 **/
@Service
public class AdminKeywordServiceImpl implements AdminKeywordService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    //list?page=1&limit=20&sort=add_time&order=desc
    @Override
    public KeywordListVO<MarketKeyword> list(KeywordListBO keywordListBO) {
        //分页插件
        PageHelper.startPage(keywordListBO.getPage(), keywordListBO.getLimit());

        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.setOrderByClause(keywordListBO.getSort() + " " + keywordListBO.getOrder());
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andDeletedEqualTo(Constant.EXIST);
        if (!StringUtil.isEmpty(keywordListBO.getKeyword())) {
            criteria.andKeywordLike("%" + keywordListBO.getKeyword() + "%");
        }
        if (!StringUtil.isEmpty(keywordListBO.getUrl())) {
            criteria.andUrlLike("%" + keywordListBO.getUrl() + "%");
        }


        //查询list集
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);

        KeywordListVO<MarketKeyword> listKeywordListVO = new KeywordListVO<>();
        PageInfo<MarketKeyword> PageInfo = new PageInfo<>(marketKeywords);
        listKeywordListVO.setLimit(PageInfo.getPageSize());
        listKeywordListVO.setPage(PageInfo.getPageNum());
        listKeywordListVO.setTotal(PageInfo.getTotal());
        listKeywordListVO.setPages(PageInfo.getPages());
        listKeywordListVO.setList(marketKeywords);

        return listKeywordListVO;
    }


    @Transactional
    @Override
    public MarketKeyword create(KeywordCreateBO keywordCreateBO) {
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setKeyword(keywordCreateBO.getKeyword());
        if(StringUtil.isNotEmpty(keywordCreateBO.getUrl())){
            marketKeyword.setUrl(keywordCreateBO.getUrl());
        }
        marketKeyword.setIsDefault(keywordCreateBO.getIsDefault());
        marketKeyword.setIsHot(keywordCreateBO.getIsHot());
        marketKeyword.setAddTime(new Date());
        marketKeyword.setUpdateTime(new Date());

        int code = marketKeywordMapper.insertSelective(marketKeyword);
        if(code == 0){
            return null;
        }

        // marketKeywordMapper.insert();

        return marketKeyword;
    }

    @Transactional
    @Override
    public MarketKeyword update(KeywordBO keywordBO) {
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setId(keywordBO.getId());
        marketKeyword.setKeyword(keywordBO.getKeyword());
        marketKeyword.setUrl(keywordBO.getUrl());
        marketKeyword.setIsHot(keywordBO.getIsHot());
        marketKeyword.setUpdateTime(new Date());
        marketKeyword.setIsDefault(keywordBO.getIsDefault());

        int code = marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
        if(code == 0){
            return null;
        }

        return marketKeyword;
    }

    @Transactional
    @Override
    public int delete(KeywordBO keywordBO) {
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setId(keywordBO.getId());
        marketKeyword.setDeleted(Constant.NOEXIST);

        int code = marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
        if(code == 0){
            return 404;
        }
        return 200;
    }
}
